package cn.com.enboga.musicbox.sys
{
	import cn.com.enboga.musicbox.model.ModelLocator;
	
	import flash.desktop.DockIcon;
	import flash.desktop.NativeApplication;
	import flash.desktop.SystemTrayIcon;
	import flash.display.Loader;
	import flash.display.NativeMenu;
	import flash.display.NativeMenuItem;
	import flash.events.Event;
	import flash.events.ScreenMouseEvent;
	import flash.net.URLRequest;
	
	import mx.controls.Alert;
	import mx.core.WindowedApplication; 
   
	public class SysTrayManage
 	{ 
 		private var musicBox:ModelLocator = ModelLocator.getInstance();
 		
		public function SysTrayManage(win:WindowedApplication):void{ 
			//NativeApplication.nativeApplication.autoExit = false; 
			var icon:Loader = new Loader(); 
			var iconMenu:NativeMenu = new NativeMenu();
			
			//添加设置按钮
			var setSystemCommand:NativeMenuItem = iconMenu.addItem(new NativeMenuItem("设置")); 
			setSystemCommand.addEventListener(Event.SELECT, function(event:Event):void { 
					Alert.show("功能实现中!"); 
				});
			
			//添加退出按钮
			var exitCommand:NativeMenuItem = iconMenu.addItem(new NativeMenuItem("退出")); 
			exitCommand.addEventListener(Event.SELECT, function(event:Event):void {
					SystemManage.saveXML(musicBox);
					NativeApplication.nativeApplication.icon.bitmaps = [];
					NativeApplication.nativeApplication.exit(); 
				}); 
	   
			if (NativeApplication.supportsSystemTrayIcon) { 
				NativeApplication.nativeApplication.autoExit = false; 
				icon.contentLoaderInfo.addEventListener(Event.COMPLETE, iconLoadComplete); 
				icon.load(new URLRequest("images/icon_016.png")); 
			  
				var systray:SystemTrayIcon =  
				NativeApplication.nativeApplication.icon as SystemTrayIcon; 
				systray.tooltip = "musicBox v0.11"; 
				systray.menu = iconMenu; 
				systray.addEventListener(ScreenMouseEvent.CLICK, function(event:Event):void { 
					if(win.nativeWindow.visible){
						//Alert.show("显示");
						win.nativeWindow.visible = false;
					}else{
						//Alert.show("隐藏");
						win.nativeWindow.visible = true;
					}
				});
			} 
		   
			if (NativeApplication.supportsDockIcon){ 
				icon.contentLoaderInfo.addEventListener(Event.COMPLETE,iconLoadComplete); 
				icon.load(new URLRequest("images/icon_016.png")); 
				var dock:DockIcon = NativeApplication.nativeApplication.icon as DockIcon;  
				dock.menu = iconMenu; 
				dock.addEventListener(ScreenMouseEvent.CLICK, function(event:Event):void { 
					Alert.show("图标"); 
				});
			}
		} 
	  
		private function iconLoadComplete(event:Event):void 
		{ 
			NativeApplication.nativeApplication.icon.bitmaps = [event.target.content.bitmapData]; 
		} 
	}
}